package fr.upmc.i3s.upmcontact.tools;

import fr.upmc.i3s.upmcontact.contents.User;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

public class PreferencesManager {
	
	private static final String TAG = "PreferencesManager";
	
	private static final String APPLICATION_PREFERENCES = "fr.upmc.i3s.upmcontact";
	
	  //////////////////////
	 // SIGN IN SETTINGS //
	//////////////////////
	
	public static void saveSignInSettings(Context context, boolean keepLogged) {
		SharedPreferences.Editor editor = context.getSharedPreferences(APPLICATION_PREFERENCES, Context.MODE_PRIVATE).edit();
		editor.putBoolean("keep-signed", keepLogged);
		editor.commit();
	}
	
	public static void saveSignInSettings(Context context, User user) {
		SharedPreferences.Editor editor = context.getSharedPreferences(APPLICATION_PREFERENCES, Context.MODE_PRIVATE).edit();
		editor.putBoolean("keep-signed", user.isKeepLogged());
		if(user.isKeepLogged()) {
			editor.putString("username", user.getUsername());
			editor.putString("password", user.getPassword());
			editor.putString("email", user.getEmail());
		}
		editor.commit();
	}
	
	public static User getSignInSettings(Context context) {
		User user = new User();
		SharedPreferences prefs = context.getSharedPreferences(APPLICATION_PREFERENCES, Context.MODE_PRIVATE);
		user.setKeepLogged(prefs.getBoolean("keep-signed", false));
		user.setUsername(prefs.getString("username", null));
		user.setPassword(prefs.getString("password", null));
		user.setEmail(prefs.getString("email", null));
		return user;
	}
	
	public static boolean isKeepLogged(Context context) {
		SharedPreferences prefs = context.getSharedPreferences(APPLICATION_PREFERENCES, Context.MODE_PRIVATE);
		return prefs.getBoolean("keep-signed", false);
	}

	public static void clearSignInSettings(Context context) {
		SharedPreferences.Editor editor = context.getSharedPreferences(APPLICATION_PREFERENCES, Context.MODE_PRIVATE).edit();
		editor.putBoolean("keep-signed", false);
		editor.putString("username", null);
		editor.putString("password", null);
		editor.putString("email", null);
		editor.commit();
	}
	
	
	  //////////////////
	 // UPDATE DATES //
	//////////////////
	
	public static void storeDate(Context context, String id, long date) {
		Log.i(TAG, "storeDate.id="+id+", date="+date);
		SharedPreferences.Editor editor = context.getSharedPreferences(APPLICATION_PREFERENCES, Context.MODE_PRIVATE).edit();
		editor.putLong(id, date);
		editor.commit();
	}

	public static long retrieveDate(Context context, String id) {
		SharedPreferences prefs = context.getSharedPreferences(APPLICATION_PREFERENCES, Context.MODE_PRIVATE);
		long content = prefs.getLong(id, 0);
		Log.i(TAG, "retrieveDate.id="+id+", content="+content);
		return content;
	}
	
	  /////////////
	 // CONTENT //
	/////////////
	
	public static void saveContent(Context context, String content, String id) {
		Log.i(TAG, "saveContent.id="+id+", content="+content);
		SharedPreferences.Editor editor = context.getSharedPreferences(APPLICATION_PREFERENCES, Context.MODE_PRIVATE).edit();
		editor.putString(id, content);
		editor.commit();
	}
	
	public static String getContent(Context context, String id) {
		SharedPreferences prefs = context.getSharedPreferences(APPLICATION_PREFERENCES, Context.MODE_PRIVATE);
		String content = prefs.getString(id, "");
		Log.i(TAG, "getContent.id="+id+", content="+content);
		return content;
	}
	
	  ////////////////
	 // STATISTICS //
	////////////////
	
	private static final String INCIDENT_STATS = "prefs.incidents.stats";
	private static final String LOST_STATS = "prefs.lost.stats";
	private static final String FOUND_STATS = "prefs.found.stats";
	
	// INCREMENT
	
	public static void incrementIncidentReportStatistics(Context context) {
		increment(context, INCIDENT_STATS);
	}
	
	public static void incrementLostReportStatistics(Context context) {
		increment(context, LOST_STATS);
	}
	
	public static void incrementFoundReportStatistics(Context context) {
		increment(context, FOUND_STATS);
	}
	
	private static void increment(Context context, String id) {
		SharedPreferences.Editor editor = context.getSharedPreferences(APPLICATION_PREFERENCES, Context.MODE_PRIVATE).edit();
		editor.putInt(id, retrieveReportStatistics(context, id)+1);
		editor.commit();
	}
	
	// RETRIEVE
	
	public static int retrieveIncidentReportStatistics(Context context) {
		return retrieveReportStatistics(context, INCIDENT_STATS);
	}
	
	public static int retrieveLostReportStatistics(Context context) {
		return retrieveReportStatistics(context, LOST_STATS);
	}
	
	public static int retrieveFoundReportStatistics(Context context) {
		return retrieveReportStatistics(context, FOUND_STATS);
	}
	
	private static int retrieveReportStatistics(Context context, String id) {
		SharedPreferences prefs = context.getSharedPreferences(APPLICATION_PREFERENCES, Context.MODE_PRIVATE);
		int content = prefs.getInt(id, 0);
		Log.i(TAG, "retrieveReportStatistics.id="+id+", content="+content);
		return content;
	}

}
